﻿#region Copyright

// Majesty of Omega - The next evolution of macro management space strategy
// 
// Copyright 2007-2009 André Claaßen
// 
// This file is protected by the terms and conditions of the
// Microsoft Community License (Ms-CL 1.1), a copy of which should
// have been distributed along with this software. If not,
// you may find the license information at the following URL:
// 
// http://www.microsoft.com/resources/sharedsource/licensingbasics/communitylicense.mspx

#endregion

using System.Linq;
using Majesty_of_Omega.Config;

namespace Majesty_of_Omega.Logic.Calculate
{
    /// <summary>
    /// Looks, which at wich available applied technology projects the
    /// applied research could start.
    /// </summary>
    public class AvailableResearchCalculator : GameCalculator
    {
        /// <summary>
        /// Called when [calculate].
        /// </summary>
        /// <param name="calculateInfo">The info.</param>
        protected override void OnCalculate(CalculateInfo calculateInfo)
        {
            var researchManager = calculateInfo.Player.ResearchManager;
            var sumOfUsedAppliedResearchPoints = researchManager.SumOfAppliedResearchPoints;
            var thresholdOfAppliedResearchPoint = researchManager.TotalResearchPointsOfCurrentTurn *
                                                  SpreadSheetManager.ResearchConfiguration.ResearchDefaults.
                                                      PercentForFundamental;

            var freeAppliedResearchPoints = thresholdOfAppliedResearchPoint - sumOfUsedAppliedResearchPoints;
            while (researchManager.TechAvailableAppliedResearchs.Count > 0 )
            {
                var firstAvailableResearchItem = researchManager.TechAvailableAppliedResearchs.First();
                if (firstAvailableResearchItem.TurnCost > freeAppliedResearchPoints) break;

                researchManager.StartAppliedResearchProject(firstAvailableResearchItem, FundingLevel.Full);
            } 
        }
    }
}